/**
 *  路由权限控制
 */

import api from '../api'
import http from '../http'


const permission = {
  namespaced: true,
  state: {
    // 菜单列表(权限)
    sidebarMenuList: [],
    // 页面按钮权限
    perms: []
  },
  mutations: {
    setSidebarMenuList (state, sidebarMenuList) {
      state.sidebarMenuList = sidebarMenuList
    },
    setPerms (state, perms) {
      state.perms = perms
    }
  },
  actions: {

    // 获取左侧菜单
    getSidebarMenuList ({ commit }) {
      return new Promise(async (resolve, reject) => {
        try {
          const sidebarMenuList = await http.get(api.sidebarMenuList)
          commit('setSidebarMenuList', sidebarMenuList.data)
          resolve(sidebarMenuList.data)
        } catch (error) {
          reject(error)
        }
      })
    },


    // 获取页面按钮权限
    getPerms ({ commit }) {
      return new Promise(async (resolve, reject) => {
        try {
          const perms = await http.get(api.perms)
          commit('setPerms', perms.data)
          resolve(perms.data)
        } catch (error) {
          reject(error)
        }
      })
    }
  }
}



export default permission